/*
 * FrameDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "FrameDelegate.h"

#include "MenuBarDelegate.h"
#include "StatusBarDelegate.h"
#include "ToolBarDelegate.h"
#include "QMainWindowAdapter.h"

// FrameDelegate :: Protected Constructor. -------------------------------------

/*!
 * \brief Constructs new FrameDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Frame java object
 */
FrameDelegate::FrameDelegate(JNIEnv *env, jobject obj)
  : WindowDelegate(env, obj, new QMainWindowAdapter())
{
  // empty constructor body
}

// FrameDelegate :: Native Calls. ----------------------------------------------

/*!
 * \brief Sets menu bar.
 *
 * \param env the JNIEnv pointer
 * \param menuBarDelegate the menu bar delegate
 */
void FrameDelegate::setMenuBar(JNIEnv *env, jlong menuBarDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMenuBar, SWING_ENV_ARG, JLONG_ARG(menuBarDelegate))
  if (menuBarDelegate) {
    qMainWindow()->setMenuBar(MENU_BAR_CAST(menuBarDelegate)->qMenuBar());
    MENU_BAR_CAST(menuBarDelegate)->setParentDelegate(this);
  } else {
    qMainWindow()->setMenuBar(0);
    // note: old menu bar and menu bar delegate are deleted
  }
}

/*!
 * \brief Adds toolbar.
 *
 * \param env the JNIEnv pointer
 * \param toolBarDelegate the toolbar delegate 
 */
void FrameDelegate::addToolBar(JNIEnv *env, jlong toolBarDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(addToolBar, SWING_ENV_ARG, JLONG_ARG(toolBarDelegate))
  qMainWindow()->addToolBar(TOOL_BAR_CAST(toolBarDelegate)->qToolBar());
  TOOL_BAR_CAST(toolBarDelegate)->setParentDelegate(this);
}

/*!
 * \brief Removes the specified toolbar.
 *
 * \param env the JNIEnv pointer
 * \param toolBarDelegate the toolbar delegate 
 */
void FrameDelegate::removeToolBar(JNIEnv *env, jlong toolBarDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(removeToolBar, SWING_ENV_ARG, JLONG_ARG(toolBarDelegate))
  qMainWindow()->removeToolBar(TOOL_BAR_CAST(toolBarDelegate)->qToolBar());
  TOOL_BAR_CAST(toolBarDelegate)->setParentDelegate(0);
}

/*!
 * \brief Sets content pane
 *
 * \param env the JNIEnv pointer
 * \param contentPaneDelegate the content pane delegate
 */
void FrameDelegate::setContentPane(JNIEnv *env, jlong contentPaneDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setContentPane, SWING_ENV_ARG, JLONG_ARG(contentPaneDelegate))
  qMainWindow()->setCentralWidget(
                                CONTAINER_CAST(contentPaneDelegate)->qWidget());
  CONTAINER_CAST(contentPaneDelegate)->setParentDelegate(this);
}

/*!
 * \brief Sets status bar.
 *
 * \param env the JNIEnv pointer
 * \param statusBarDelegate the status bar delegate
 */
void FrameDelegate::setStatusBar(JNIEnv *env, jlong statusBarDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setStatusBar, SWING_ENV_ARG, JLONG_ARG(statusBarDelegate))
  if (statusBarDelegate) {
    qMainWindow()->setStatusBar(STATUS_BAR_CAST(statusBarDelegate)->qStatusBar());
    STATUS_BAR_CAST(statusBarDelegate)->setParentDelegate(this);
  } else {
    qMainWindow()->setStatusBar(0);
    // note: old status bar and status bar delegate are deleted
  }
}

// FrameDelegate :: Static Native Calls. ---------------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong FrameDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new FrameDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
